<?php

namespace App\Recharge\Api;

use App\Models\InterfaceConfiguration;
use App\Recharge\Bean\ApiNotifyDataBean;
use App\Recharge\Bean\BasicInformation;
use Illuminate\Http\Request;

class Lechong extends AbstractApi
{

    /**
     * @inheritDoc
     */
    public function rechargeData(BasicInformation $bean): array
    {
        $userName = base64_encode($bean->getAccount());
        $array = [
            'agentcode' => $bean->getSupplierNum(), //商户号
            'code'      => $bean->getGoodsId(), //商品编号id,
            'time'      => date('YmdHis'),
            'account'   => $userName,
            'sellerid'  => $bean->getOrderNo(), //订单号
            'num'       => $bean->getRechargeNum() * $bean->getStartCharging(),
            'value'     => $bean->getFaceValue() * $bean->getRechargeNum() * $bean->getStartCharging(),
            'notifyurl' => $bean->getNotifyUrl(), //异步回调地址
            'remark'    => urlencode(base64_encode('')),
        ];
        $array['sign'] = md5($array['agentcode'].$array['sellerid'].$array['account'].$array['code']
            .$array['num'].$array['value'].$array['notifyurl'].$array['remark']
            .$array['time']. $bean->getSecretKey());

        return $array;
    }

    /**
     * @param InterfaceConfiguration $api
     * @param Request $request
     * @return ApiNotifyDataBean
     */
    public function apiNotifyLogic(InterfaceConfiguration $api, Request $request): ApiNotifyDataBean
    {
        $status = $request->input('state', '');
        $order_sn = $request->input('sellerid', '');
        return $this->apiNotifyData($order_sn, $status)->setExecute(in_array($status, ['2', '4']))->setRechargeSuccess($status == '2');
    }

    /**
     * @return mixed
     */
    public function apiNotifySuccess(): mixed
    {
        return response('OK');
    }
}
